﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 29/03/2014
 * Time: 04:46 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
	/// <summary>
	/// Description of paisdaimpl.
	/// </summary>
	public class paisdaimpl:Gestión_UTN.da.paisda
	{
		public paisdaimpl()
		{
		}
		
		public bool SavePais(string nombre)
		{
			bool ingresado = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "INSERT INTO pai_paises(pai_nombre)" +
                    			"VALUES(@pai_nombre)";
					
					OleDbCommand insertar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					insertar.Parameters.AddWithValue("@pai_nombre",nombre);
					
					insertar.ExecuteNonQuery();
					
					trans.Commit();
					
					ingresado = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				ingresado = false;
				trans.Rollback();
				conex.Close();
			}
			
			return ingresado;
		}
		
		public bool DeletePais(string nombre)
		{
			bool eliminado = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "DELETE FROM pai_paises WHERE pai_nombre = @pai_nombre";
					
					OleDbCommand eliminar = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					trans = conex.BeginTransaction();
					
					eliminar.Parameters.AddWithValue("@pai_nombre",nombre);
					
					eliminar.ExecuteNonQuery();
					
					trans.Commit();
					
					eliminado = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				eliminado = false;
				trans.Rollback();
				conex.Close();
			}
			
			return eliminado;
		}
		
		public bool ModifyPais(string nombre_anterior, string nombre)
		{
			bool modificado = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "UPDATE pai_paises SET pai_nombre = @pai_nombre," +
									"WHERE pai_nombre = @pai_nombre_anterior";
					
					OleDbCommand modificar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {nombre,nombre_anterior};
					
					modificar.Parameters.AddRange(values);
					
					modificar.ExecuteNonQuery();
					
					trans.Commit();
					
					modificado = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				modificado = false;
				trans.Rollback();
				conex.Close();
			}
			
			return modificado;
		}
		
		public System.Data.DataTable FindAllPaises()
		{
			DataTable tablaPaises = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				tablaPaises = new DataTable();
				
				if (null != conex) {
					
					string sql = "SELECT pai_id, pai_nombre" +
									" FROM pai_paises pai";
					
					 OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);
					 
					 conex.Open();
					 
					 adaptador.Fill(tablaPaises,"paises");
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				tablaPaises = null;
				conex.Close();
			}
			
			return tablaPaises;
		}
		
		public Gestión_UTN.Entities.Pais FindPaisByName(string nombre)
		{
			Pais pais = null;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "SELECT pai_id, pai_nombre" +
									" FROM pai_paises pai" +
									" WHERE pai_nombre = @pai_nombre";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					 
					consulta.Parameters.AddWithValue("@pai_nombre",nombre);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					pais = new Pais(Convert.ToInt16(lector[0].ToString()),lector[1].ToString());
					conex.Close();
				}
				
			} catch (Exception) {
				
				trans.Rollback();
				pais = null;
				conex.Close();
			}
			
			return pais;
		}
		
		public Pais FindPaisById(int id)
		{
			Pais pais = null;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "SELECT pai_id, pai_nombre" +
									" FROM pai_paises pai" +
									" WHERE pai_id = @pai_id";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					 
					consulta.Parameters.AddWithValue("@pai_id",id);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					pais = new Pais(Convert.ToInt16(lector[0].ToString()),lector[1].ToString());
					conex.Close();
				}
				
			} catch (Exception) {
				
				trans.Rollback();
				pais = null;
				conex.Close();
			}
			
			return pais;
		}
	}
}
